package com.risk.kernel.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.risk.kernel.domain.TkTaskCaseResult;
import com.risk.kernel.mapper.TkTaskCaseResultMapper;
import com.risk.kernel.service.TkTaskCaseResultServiceI;
import com.risk.kernel.vo.TkTaskCaseInfoVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author: Liwen
 * @createTime: 2025/1/23
 * @description:
 */
@Service
public class TkTaskCaseResultServiceImpl implements TkTaskCaseResultServiceI {
    @Autowired
    private TkTaskCaseResultMapper tkTaskCaseResultMapper;

    /**
     * 查询
     */
    @Override
    public TkTaskCaseResult selectTkTaskCaseResultById(String id) {
        return tkTaskCaseResultMapper.selectTkTaskCaseResultById(id);
    }

    /**
     * 查询列表
     */
    @Override
    public List<TkTaskCaseInfoVo> selectTkTaskCaseResultList(String rid) {


        List<TkTaskCaseResult> tkTaskCaseResults = tkTaskCaseResultMapper.selectTkTaskCaseResultList(rid);
        List<TkTaskCaseInfoVo> resultList = tkTaskCaseResults.stream().map(item -> {
            TkTaskCaseInfoVo tkTaskCaseInfoVo = new TkTaskCaseInfoVo();
            BeanUtils.copyProperties(item, tkTaskCaseInfoVo);
            tkTaskCaseInfoVo.setExecContent(JSONObject.parseObject(item.getExecContent()));
            return tkTaskCaseInfoVo;
        }).collect(Collectors.toList());
        return resultList;
    }

    /**
     * 新增【请填写功能名称】
     */
    @Override
    public int insertTkTaskCaseResult(TkTaskCaseResult tkTaskCaseResult) {
        return tkTaskCaseResultMapper.insertTkTaskCaseResult(tkTaskCaseResult);
    }

    /**
     * 修改
     */
    @Override
    public int updateTkTaskCaseResult(TkTaskCaseResult tkTaskCaseResult) {
        return tkTaskCaseResultMapper.updateTkTaskCaseResult(tkTaskCaseResult);
    }

    /**
     * 批量删除
     */
    @Override
    public int deleteTkTaskCaseResultByIds(String resultId) {
        return tkTaskCaseResultMapper.deleteTkTaskCaseResultByIds(resultId);
    }

    /**
     * 删除
     */
    @Override
    public int deleteTkTaskCaseResultById(String id) {
        return tkTaskCaseResultMapper.deleteTkTaskCaseResultById(id);
    }

    @Override
    public List<JSONObject> getCaseResultName(String caseId) {
        List<TkTaskCaseResult> caseResultNameList = tkTaskCaseResultMapper.getCaseResultName(caseId).stream().sorted(Comparator.comparing(TkTaskCaseResult::getCaseName).reversed()).collect(Collectors.toList());
//        List<TkTaskCaseResult> caseResultNameList = tkTaskCaseResultMapper.getCaseResultName(caseId);
        List<JSONObject> list = new ArrayList<>();
        if (!CollectionUtils.isEmpty(caseResultNameList)) {
            list = caseResultNameList.stream().map(item -> {
                JSONObject jsonObject = new JSONObject();
                jsonObject.put("resultId", item.getResultId());
                jsonObject.put("caseName", item.getCaseName());
                return jsonObject;
            }).collect(Collectors.toList());
        }
        return list;
    }
}
